package org.noote.test_voxel;

import javax.microedition.khronos.opengles.GL10;

import org.noote.libs.sogle.Object3D_Group;
import org.noote.libs.sogle.Renderer;
import org.noote.libs.sogle.Vector3D;
import org.noote.libs.voxel.MeshBuilder;
import org.noote.libs.voxel.Sprite3D;

import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.MotionEvent;

public class TestVoxel_Activity extends Activity {

	 RenderOGL mRenderer;
	    
	    public class RenderOGL extends Renderer {

		       public RenderOGL(Context context)
		        {
		        	super(context);
		        }
			
		        @Override
		        public void onCreateScene(GL10 gl)
		    	{
		    		super.onCreateScene(gl);
		    		
		    		setBackGroundColor(0.5f, 0.5f, 1.0f);
		    		_camera.setLookAt(new Vector3D(0, 0, -25), new Vector3D(0, 0, 1), 0);
		    		lighting(false);
		    		
		    		MeshBuilder builder = new MeshBuilder();
		            
		            byte vMin = 127;
		            byte vMax = -127;
		            Sprite3D o = new Sprite3D();
		            o.createVolume(5, 5, 5, vMin);
		            o.setVoxel(0, 0, 0, vMax);
		            o.setVoxel(0, 1, 0, vMax);
		            o.setVoxel(1, 1, 0, vMax);
		            o.setVoxel(1, 1, 1, vMax);
		            o.setVoxel(1, 1, 2, vMax);
		            o.setVoxel(1, 2, 2, vMax);
		            o.setVoxel(1, 2, 3, vMax);
		            o.setVoxel(2, 2, 3, vMax);
		            o.setVoxel(2, 2, 4, vMax);
		            o.setVoxel(2, 3, 4, vMax);
		            o.setVoxel(2, 4, 4, vMax);
		            o.setVoxel(3, 4, 4, vMax);
		            
		            o.setColor(1, 0, 0, 1);
		            Object3D_Group mg=builder.build(o, (byte)0);
		            mg.setPosition(0, 0, 0);
		            _listObjects.add(mg);

		            o.setColor(0, 1, 0, 1);
		            Object3D_Group mgt=builder.buildByTetrahedon(o, (byte)0);
		            mgt.setPosition(-5, 0, 0);
		            _listObjects.add(mgt);

		            o.setColor(0, 0, 1, 1);        
		            Object3D_Group mgc=builder.buildAsCubes(o, (byte)0);
		            mgc.setPosition(5, 0, 0);
		            _listObjects.add(mgc);
		    	}
		}
	
	class ClearGLSurfaceView extends GLSurfaceView {
	    public ClearGLSurfaceView(Context context) {
	        super(context);
	        mRenderer = new RenderOGL(context);
	        setRenderer(mRenderer);
	    }

	    @Override
		public boolean onTouchEvent(final MotionEvent event) {
	        queueEvent(new Runnable(){
	            @Override
				public void run() {

	            	float x = event.getX()/getWidth();
	            	float y = event.getY()/getHeight();
	            	
	            	if(y>0.66 && x>0.33 && x<0.66)
	            		mRenderer._camera.move_forward(-0.125f);
	            	else if(y<0.33 && x>0.33 && x<0.66)
	            		mRenderer._camera.move_forward(0.125f);
	            	else if(x>0.66 && y>0.33 && y<0.66)
	            		mRenderer._camera.move_left(0.125f);
	            	else if(x<0.33 && y>0.33 && y<0.66)
	            		mRenderer._camera.move_left(-0.125f);            		
	            	else if(x>0.66 && y>0.66)
	            		mRenderer._camera.rotate_view_left(0.0125f);
	            	else if(x<0.33 && y>0.66)
	            		mRenderer._camera.rotate_view_left(-0.0125f);            		
	            }
            });
            return true;
        }
	}

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mGLView = new ClearGLSurfaceView(this);
        setContentView(mGLView);
	}
    
    @Override
    protected void onPause() {
        super.onPause();
        mGLView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mGLView.onResume();
    }

    private GLSurfaceView mGLView;
}